﻿using NoteBook_Shared.Contact;
using NoteBook_Shared.Parameters;

namespace NoteBook_Product.Services
{
    public class BaseService<TEntity> : IBaseService<TEntity> where TEntity : class
    {
        public readonly HttpRestClient _client;
        public readonly string ServiceName;

        public BaseService(HttpRestClient client, string serviceName)
        {
            _client = client;
            ServiceName = serviceName;
        }

        public async Task<ApiResponse<TEntity>> AddAsync(TEntity entity)
        {
            BaseRequest baseRequest = new BaseRequest();
            baseRequest.Method = RestSharp.Method.POST;
            baseRequest.Rotue = $"api/{ServiceName}/Add";
            baseRequest.Parameter = entity;
            return await _client.ExecuteAsync<TEntity>(baseRequest);
        }

        public async Task<ApiResponse> DeleteAsync(int id)
        {
            BaseRequest baseRequest = new BaseRequest();
            baseRequest.Method = RestSharp.Method.DELETE;
            baseRequest.Rotue = $"api/{ServiceName}/Delete?id={id}";
            return await _client.ExecuteAsync(baseRequest);
        }

        public async Task<ApiResponse<PagedList<TEntity>>> GetAllAsync(QueryParameter queryParameter)
        {
            BaseRequest baseRequest = new BaseRequest();
            baseRequest.Method = RestSharp.Method.GET;
            baseRequest.Rotue = $"api/{ServiceName}/GetAll?pageIndex={queryParameter.PageIndex}&pageSize={queryParameter.PageSize}&search={queryParameter.Search}";
            return await _client.ExecuteAsync<PagedList<TEntity>>(baseRequest);
        }

        public async Task<ApiResponse<TEntity>> GetFirstOfDefaultAsync(int id)
        {
            BaseRequest baseRequest = new BaseRequest();
            baseRequest.Method = RestSharp.Method.GET;
            baseRequest.Rotue = $"api/{ServiceName}/Get?id={id}";
            return await _client.ExecuteAsync<TEntity>(baseRequest);
        }

        public async Task<ApiResponse<TEntity>> UpdateAsync(TEntity entity)
        {
            BaseRequest baseRequest = new BaseRequest();
            baseRequest.Method = RestSharp.Method.POST;
            baseRequest.Rotue = $"api/{ServiceName}/Update";
            baseRequest.Parameter = entity;
            return await _client.ExecuteAsync<TEntity>(baseRequest);
        }
    }
}
